import 'package:flutter/material.dart';
import 'package:x3_base_package/x3_base_package.dart';

class BusinessCustomerDetailsScore extends StatelessWidget {

  static final String sName = 'vip/myscore';

  @override
  Widget build( BuildContext context ) {
    dynamic arguments = BaseRouterUtil.getArguments(context);
    String vipId = BaseUtils.getKeyData(arguments, 'vipId').toString();
    return BaseScaffoldComponent(
      title: '积分',
      child: _buildMainWidget(context, vipId),
    );
  }

  Widget _buildMainWidget( BuildContext context, String vipId ) {
    return Column(
      children: [
        BaseCardComponent(child: _buildScoreTotalContainer(context, vipId)),
        Expanded(child: _buildScoreList(context, vipId))
      ],
    );
  }

  Widget _buildScoreTotalContainer( BuildContext context, String vipId ) {
    String service = '/x3-service-vip-center/vip/customer/query/runwater/scoretab/total';
    Map<String, dynamic> params = { 'vipId': vipId };
    return BaseScreenRefreshComponent(
      request: BaseScreenRequestModel(service, params: params),
      builder: (BaseHttpResultModel result) => this._buildScoreTotal( context, result ),
    );
  }

  Widget _buildScoreTotal( BuildContext context, BaseHttpResultModel result ) {
    String rid = BaseUtils.getKeyData(result.data, 'rid').toString();
    String total = BaseUtils.getKeyData(result.data, 'total').toString();
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10.0),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(BaseIconData.x3Score, color: Colors.orange, size: 36.0),
          Text.rich(TextSpan(style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 16.0, fontWeight: FontWeight.bold), text: '$rid:', children: [WidgetSpan(child: SizedBox(width: 10.0)), TextSpan(text: total)])),
        ],
      ),
    );
  }

  Widget _buildScoreList( BuildContext context, String vipId ) {
    String service = '/x3-service-vip-center/vip/customer/query/runwater/scoretab';
    Map<String, dynamic> params = { 'vipId': vipId };
    return BaseRefreshComponent(path: service, buildItem: (Map<String, dynamic> item, int index) => _buildScoreListItem(context, item, index), params: params);
  }

  Widget _buildScoreListItem(BuildContext context, Map<String, dynamic> item, int index) {
    String scoreChange = BaseUtils.getKeyData(item, 'scoreChange').toString();
    num scoreChangeNumber = scoreChange.length == 0 ? 0 : num.parse(scoreChange);
    TextStyle textStyle = scoreChangeNumber < 0 ? Theme.of(context).textTheme.bodyText2 : Theme.of(context).textTheme.bodyText1;
    return BaseCardComponent( circular: 4.0, child: Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Expanded(child: this._buildScoreListItemContent(context, item, index)),
        Text(scoreChange, style: textStyle.copyWith(fontSize: 16.0)),
      ],
    ) );
  }

  Widget _buildScoreListItemContent(BuildContext context, Map<String, dynamic> item, int index) {
    String subjectName = BaseUtils.getKeyData(item, 'subjectName').toString();
    String scoreChangeDate = BaseUtils.getKeyData(item, 'scoreChangeDate').toString();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(subjectName, style: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 14.0)),
        SizedBox(height: 4.0),
        Text(BaseTransformUtil.dateToString(dateTime: scoreChangeDate), style: Theme.of(context).textTheme.subtitle2),
      ],
    );
  }

}
